home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 53003 / 53003.xpi / chrome / flashVideoDownload.jar / content / overlay.js next >
Text File  |  2009-12-04  |  12KB  |  336 lines

  1. var flashVideoDownload;
  2. flashVideoDownload = new Object();
  3.  
  4. flashVideoDownload.initialization = function() 
  5. {
  6.     window.addEventListener("load", flashVideoDownload.onWindowLoad, false);
  7.     window.addEventListener("unload", flashVideoDownload.onWindowUnload, false);
  8. }
  9. flashVideoDownload.onWindowLoad = function() 
  10. {
  11.     var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  12.     observerService.addObserver(flashVideoDownload.flashVideoDownloadObserver, "http-on-examine-response", false);
  13.     observerService.addObserver(flashVideoDownload.flashVideoDownloadObserver, "http-on-modify-request", false);
  14.     observerService.addObserver(flashVideoDownload.flashVideoDownloadObserver, "http-on-examine-cached-response", false);
  15.  
  16.     var listener = {
  17.       onStatusChange: function() {},
  18.       onStateChange: function() {},
  19.       onLocationChange: function(aProgress, aRequest, aURI) {
  20.           flashVideoDownload.clearStatusBar();
  21.           if (aURI==null) {
  22.             document.getElementById("flashVideoDownloadStatusBarIcon").setAttribute("class", "flashVideoDownloadIconDisabled");
  23.           } else {
  24.             flashVideoDownload.setStatusBar(getBrowser().selectedBrowser.contentDocument)
  25.           }
  26.       },
  27.       onProgressChange: function() {},
  28.       onSecurityChange: function() {}
  29.     };
  30.     getBrowser().addProgressListener(listener, Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);
  31. }
  32. flashVideoDownload.onWindowUnload = function() 
  33. {
  34.     var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  35.     observerService.removeObserver(flashVideoDownload.flashVideoDownloadObserver, "http-on-examine-response", false);
  36.     observerService.removeObserver(flashVideoDownload.flashVideoDownloadObserver, "http-on-modify-request", false);
  37.     observerService.removeObserver(flashVideoDownload.flashVideoDownloadObserver, "http-on-examine-cached-response", false);
  38. }
  39. flashVideoDownload.buttonPressed = function(e) 
  40. {
  41.     var popup = document.getElementById("flashVideoDownloadContextMenu");
  42.     popup.showPopup(e.target, -1, -1, "popup", "bottomleft", "topleft");
  43. }
  44. flashVideoDownload.flashVideoDownloadObserver = 
  45. {
  46.       observe: function(aSubject, aTopic, aData) {
  47.         if ((aTopic=="http-on-examine-response") || (aTopic=="http-on-modify-request")
  48.             || (aTopic=="http-on-examine-cached-response")){
  49.             try {
  50.                 aSubject.QueryInterface(Components.interfaces.nsIRequest);
  51.                 var url              = aSubject.name;
  52.                 var contentType   = aSubject.contentType;
  53.                 var contentLength = aSubject.contentLength;
  54.                 var fileType = flashVideoDownload.getFileType(contentType, url);
  55.                 aSubject.QueryInterface(Components.interfaces.nsIChannel); 
  56.  
  57.                 if ((fileType=="swf") || (fileType=="flv") || (fileType=="mp4")) {
  58.                             var browser = flashVideoDownload.getBrowser(aSubject, aSubject);
  59.                             var doc    = browser.contentDocument;
  60.                             var title  = doc.title;
  61.                             var domain = doc.domain;
  62.                             var href   = doc.location.href;
  63.  
  64.                             flashVideoDownload.addFile(doc, {
  65.                                     title          : title,
  66.                                     domain          : domain,
  67.                                     href          : href,
  68.                                     fileType      : fileType,
  69.                                     contentType   : contentType,
  70.                                     contentLength : contentLength,
  71.                                     url              : url,
  72.                                     doc           : doc,
  73.                                     icon          :    flashVideoDownload.getIcon(doc)
  74.                             });
  75.                         //    dumb(contentType+" "+url);
  76.                 }
  77.             } catch(ex) {
  78.             }    
  79.         }  else {
  80.         }
  81.         return true
  82.       },
  83.  
  84.       QueryInterface: function(aIID) {
  85.         if (aIID.equals(Components.interfaces.nsISupports) ||
  86.             aIID.equals(Components.interfaces.nsIObserver) )
  87.           return this;
  88.         throw Components.results.NS_NOINTERFACE;
  89.       }
  90. }
  91. flashVideoDownload.getIcon = function (doc) 
  92. {
  93.     var favIcon = "";
  94.     var linkEl = doc.getElementsByTagName("link");
  95.     for (var i=0;i<linkEl.length;i++) {
  96.             if (linkEl[i].getAttribute("rel")) {
  97.                 if (linkEl[i].getAttribute("rel").toLowerCase()=="shortcut icon") {
  98.                    favIcon = linkEl[i].href
  99.                    break;
  100.                 }
  101.             }
  102.     }
  103.     return favIcon;
  104. }
  105. flashVideoDownload.getBrowser = function(aChannel, aSubject)
  106. {
  107.   try {
  108.     var notificationCallbacks = 
  109.       aChannel.notificationCallbacks ? aChannel.notificationCallbacks : aSubject.loadGroup.notificationCallbacks;
  110.  
  111.     if (!notificationCallbacks)
  112.       return null;
  113.  
  114.     var callback = notificationCallbacks.getInterface(Components.interfaces.nsIDOMWindow);
  115.     return callback.top.document ? 
  116.       gBrowser.getBrowserForDocument(callback.top.document) : null;
  117.   }
  118.   catch(e) {
  119.     return null;
  120.   }
  121. }
  122. flashVideoDownload.addFile = function(doc, params) 
  123. {
  124.     if (doc.swfList==null) doc.swfList = new Array();
  125.     if (doc.flvList==null) doc.flvList  = new Array();
  126.     if (params.fileType=="swf") flashVideoDownload.addToList(doc.swfList ,params);
  127.     if ((params.fileType=="flv") || (params.fileType=="mp4")) flashVideoDownload.addToList(doc.flvList ,params);
  128.     flashVideoDownload.setStatusBar(doc);
  129. }
  130. flashVideoDownload.addToList = function(list, params) 
  131. {
  132.     for (var i=0;i<list.length;i++) {
  133.         if (list[i].url==params.url) return false;
  134.     }
  135.     list.push(params);
  136. }
  137. flashVideoDownload.clearStatusBar = function() 
  138. {
  139.     var flashVideoDownloadContextMenu    = document.getElementById("flashVideoDownloadContextMenu");
  140.     while (flashVideoDownloadContextMenu.firstChild) {
  141.       flashVideoDownloadContextMenu.removeChild(flashVideoDownloadContextMenu.firstChild);
  142.     }    
  143. }
  144. flashVideoDownload.setStatusBar = function(doc) 
  145. {
  146.     if (getBrowser().selectedBrowser.contentDocument!=doc) return false;
  147.     if (doc.swfList==null) doc.swfList = new Array();
  148.     if (doc.flvList==null) doc.flvList  = new Array();
  149.  
  150.     var flashVideoDownloadContextMenu    = document.getElementById("flashVideoDownloadContextMenu");
  151.  
  152.     while (flashVideoDownloadContextMenu.firstChild) {
  153.       flashVideoDownloadContextMenu.removeChild(flashVideoDownloadContextMenu.firstChild);
  154.     }    
  155.  
  156.     if (doc.swfList.length>0) {
  157.         for (var i=0;i<doc.swfList.length;i++) {
  158.             var menuItem = flashVideoDownload.createMenuItem(doc.swfList[i]);
  159.             flashVideoDownloadContextMenu.appendChild(menuItem);
  160.         }
  161.     }
  162.     if (doc.flvList.length>0) {
  163.         if (doc.swfList.length>0) {
  164.             var separator = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "xul:menuseparator");
  165.             flashVideoDownloadContextMenu.appendChild(separator);
  166.         }
  167.         if (doc.flvList.length>0) {
  168.             var menuitem = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "xul:menuitem");
  169.             menuitem.setAttribute("label", "Videos to Dwonload");
  170.             menuitem.style.fontWeight = "bold"
  171.             flashVideoDownloadContextMenu.appendChild(menuitem);
  172.         }
  173.         for (var i=0;i<doc.flvList.length;i++) {
  174.  
  175.             var menuItem = flashVideoDownload.createMenuItem(doc.flvList[i]);
  176.             menuItem.setAttribute("class", "menuitem-iconic");
  177.             flashVideoDownloadContextMenu.appendChild(menuItem);
  178.         }
  179.     }
  180.     if ((doc.swfList.length>0) || (doc.flvList.length>0)) {
  181.         
  182.         if (doc.flvList.length>0) {
  183.             document.getElementById("flashVideoDownloadStatusBarIcon").setAttribute("class", "flashVideoDownloadIconEnabledVideo");
  184.         } else {
  185.             document.getElementById("flashVideoDownloadStatusBarIcon").setAttribute("class", "flashVideoDownloadIconEnabled");
  186.         }
  187.     } else {
  188.         document.getElementById("flashVideoDownloadStatusBarIcon").setAttribute("class", "flashVideoDownloadIconDisabled");
  189.     }
  190. }
  191. flashVideoDownload.createMenuItem = function(params) 
  192. {
  193.     var fileType = params.fileType;
  194.     var label    = params.url;
  195.     if ((fileType=="flv") || (fileType=="mp4")) {
  196.         label = params.title + "." + fileType;
  197.     } else {
  198.         var dotPos = label.lastIndexOf("." + fileType)
  199.         if (dotPos!=-1) {
  200.             var str   = label.substr(1, dotPos+3);  
  201.             var pos = str.lastIndexOf("/");
  202.             if (pos!=-1) {
  203.                 var label = str.substr(pos+1);  
  204.                 if (label) {
  205.                     
  206.                 }
  207.             }
  208.         }
  209.     }
  210.     var menuitem = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "xul:menuitem");
  211.     
  212.     menuitem.setAttribute("type",  params.fileType);
  213.     menuitem.setAttribute("label", label);
  214.     menuitem.setAttribute("url",   params.url);
  215.     menuitem.setAttribute("tooltiptext", params.url);
  216.     menuitem.setAttribute("image", params.icon);
  217.     menuitem.addEventListener("click", function(e) {
  218.         var t = e.currentTarget;
  219.         flashVideoDownload.downloadFile(t.getAttribute("label"), t.getAttribute("url"), t.getAttribute("type"));
  220.     },false);
  221.     menuitem.addEventListener("mouseover", function(e) {
  222.         var t = e.currentTarget;
  223.         flashVideoDownload.showHideElement(params.doc, t.getAttribute("url"), true);
  224.     },false);
  225.     menuitem.addEventListener("mouseout", function(e) {
  226.         var t = e.currentTarget;
  227.         flashVideoDownload.showHideElement(params.doc, t.getAttribute("url"), false);
  228.     },false);
  229.  
  230.     return menuitem;
  231. }
  232. flashVideoDownload.getElPos = function(domElement) 
  233. {
  234.     var position = {
  235.                     x: 0, 
  236.                     y: 0
  237.                    } 
  238.     while ((domElement.offsetParent) && (domElement.tagName.toLowerCase()!='body')) {
  239.         position.x += domElement.offsetLeft;
  240.         position.y += domElement.offsetTop;
  241.         domElement = domElement.offsetParent;
  242.     }
  243.     position.x += domElement.offsetLeft;
  244.     position.y += domElement.offsetTop;
  245.     return position;
  246. }
  247. flashVideoDownload.showHideElement = function(doc, src, isShow)
  248. {
  249.      var embedList =  doc.getElementsByTagName("embed");
  250.      if (embedList.length>0) {
  251.         for (var i=0;i<embedList.length;i++) {
  252.             if (embedList[i].src==src) {
  253.                 if (isShow) {
  254.                     var _body = doc.getElementsByTagName("body")[0];
  255.                     if (doc.floatElement==null) {
  256.                         doc.floatElement = doc.createElement("div");
  257.                         _body.appendChild(doc.floatElement);
  258.                     }
  259.                     var div = doc.floatElement;
  260.                     div.style.position = "absolute";
  261.                     div.style.zIndex = "10000"
  262.                     var pos = flashVideoDownload.getElPos(embedList[i]);
  263.                     div.style.left   = (pos.x-4) + "px";
  264.                     div.style.top    = (pos.y-4) + "px";
  265.  
  266.                     div.style.width  = (embedList[i].clientWidth) + "px";
  267.                     div.style.height = (embedList[i].clientHeight) + "px";
  268.  
  269.                     div.style.border = "4px solid #ff0000"
  270.                     div.style.display='';
  271.                 } else {
  272.                     doc.floatElement.style.display='none';
  273.                 }
  274.             }
  275.         }
  276.      }
  277. }
  278. flashVideoDownload.getDownloadFile = function(defaultString, fileType) 
  279. {
  280.     var nsIFilePicker = Components.interfaces.nsIFilePicker;
  281.  
  282.     var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  283.     fp.init(window, "Save As", nsIFilePicker.modeSave);
  284.     try {
  285.         var urlExt = defaultString.substr(defaultString.lastIndexOf(".")+1, 3);
  286.         if (urlExt!=fileType) defaultString += "." + fileType
  287.     }catch(ex){}
  288.  
  289.     fp.defaultString = defaultString;
  290.  
  291.     fp.appendFilter(fileType, "*." + fileType);
  292.     var rv = fp.show();
  293.     if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
  294.       var file = fp.file;
  295.       var path = fp.file.path;
  296.       return file;
  297.     }
  298.     return null;
  299. }
  300. flashVideoDownload.downloadFile = function(title, url, fileType) 
  301. {
  302.     var file = flashVideoDownload.getDownloadFile(title, fileType);
  303.  
  304.     var persist = Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1'].createInstance(Components.interfaces.nsIWebBrowserPersist);  
  305.     var ios = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);  
  306.     var uri = ios.newURI(url, null, null); 
  307.     
  308.     var target = ios.newFileURI(file) 
  309.     var xfer = Components.classes["@mozilla.org/transfer;1"]  
  310.                            .createInstance(Components.interfaces.nsITransfer);  
  311.     xfer.init(uri, target, "", null, null, null, persist);  
  312.     persist.progressListener = xfer; 
  313.     persist.saveURI(uri, null, null, null, null, file);
  314.  
  315. }
  316. flashVideoDownload.getFileType = function(contentType, url) 
  317. {
  318.     if (contentType=="application/x-shockwave-flash") {
  319.         return "swf";
  320.     }
  321.  
  322.     if ((contentType=="video/x-flv") || (contentType=="video/flv")) {
  323.         return "flv";
  324.     } else {
  325.         var urlExt = url.substr(url.lastIndexOf(".")+1, 3);  
  326.         if (urlExt=="flv") {
  327.             return "flv";
  328.         }
  329.     }
  330.     if (contentType=="video/mp4") {
  331.         return "mp4";
  332.     }
  333.     return "";
  334. }
  335. flashVideoDownload.initialization();
  336.